<?php
/**
 *APINAME:
 *Developer:
 *Complete:
 */

namespace app\index\controller;


use app\index\common\BaseController;
use app\index\model\Option;
use app\index\model\Project;
use app\index\validate\Vali;
use think\facade\Request;

class Admin extends BaseController
{
    /**
     * 添加投票项目
     * @param Vali $validate
     * @param Project $project
     * @param Option $option
     * @return \think\response\Json
     */
    public function addProject(Vali $validate , Project $project , Option $option){
        $data = Request::param();
        $img = Request::file('img');
        if (!$validate->scene('addProject')->batch()->check($data)) return $this->returnJson(self::ERROR,'数据格式错误',$validate->getError());
        if (!$validate->fileExt($img,['jpg','png','jpeg'])) return $this->returnJson(self::ERROR,'图片格式错误或没上传图片');
        $userId = $this->getUserId();               // 获取投票发起人的id
        $info = $img->move("uploads");
        $save = "http://" . Request::server("HTTP_HOST") . "\\" . "uploads\\" . $info->getSaveName();

        //项目表的数据
        $proData = [
            'user_id'                     =>                  $userId,
            'name'                        =>                  $data['ProName'],
            'content'                     =>                  $data['ProContent'],
            'poll_number'             =>                  $data['ProNum'],
            'start_time'                 =>                  $data['ProStart'],
            'end_time'                  =>                  $data['ProEndTime'],
            'img'                           =>                  $save
        ];
        $project->insert($proData);
        $proId = $project->getLastInsID();

        //数组转换操作处理
        $result = explode(',',$data['ProOpt']);
        $result[0] = substr($result[0],1);
        $result[count($result)-1] = substr($result[count($result)-1],0,-1);
        $data['ProOpt'] = $result;

        //选项表的数据
        foreach ($data['ProOpt'] as $value){
            $option->insert(['project_id' => $proId , 'content' => $value]);
        }
        return $this->returnJson(self::OK,'添加投票项目成功');
    }

    /**
     * 删除投票项目
     * @param Project $project
     * @param Option $option
     * @return \think\response\Json
     * @throws \Exception
     */
    public function proDelete(Project $project , Option $option){
        $proId = Request::param('id');
        $project->where('id','=',$proId)->delete();
        $option->where('project_id','=',$proId)->delete();
        return $this->returnJson(self::OK,'删除投票项目成功');
    }

    /**
     * 查询管理员添加的项目接口
     * @param Project $project
     * @return \think\response\Json
     */
    public function proCheck(Project $project){
        $userId = $this->getUserId();
        $adminPro = $project->field(['id','name','FROM_UNIXTIME(end_time,"%Y-%m-%d %H%i:%s") as end_time','img'])->where('user_id','=',$userId)->select();
        return $this->returnJson(self::OK,'返回管理员添加的项目',$adminPro);
    }

    /**
     * 修改投票项目的接口
     * @param Vali $validate
     * @param Project $project
     * @param Option $option
     * @return \think\response\Json
     */
    public function proEdit(Vali $validate , Project $project , Option $option){
        $data = Request::param();
        $userId = $this->getUserId();               // 获取投票发起人的id
        if (!$validate->scene('addProject')->batch()->check($data)) return $this->returnJson(self::ERROR,'数据格式错误',$validate->getError());
        $result =  $project->where('id','=',$data['id'])->field('start_time')->find()['start_time'];
        if ($result < time()) return $this->returnJson(self::ERROR,'投票已开始，不能进行修改');

        //项目表的数据
        $proData = [
            'user_id'                     =>                  $userId,
            'name'                        =>                  $data['ProName'],
            'content'                     =>                  $data['ProContent'],
            'poll_number'             =>                  $data['ProNum'],
            'start_time'                 =>                  $data['ProStart'],
            'end_time'                  =>                  $data['ProEndTime'],
        ];

        //判断是否更新图片，对图片进行处理
        $img = Request::file('img');
        if (!empty($img)){
            if (!$validate->fileExt($img,['jpg','png','jpeg'])) return $this->returnJson(self::ERROR,'图片格式错误');
            $info = $img->move("uploads");
            $save = "http://" . Request::server("HTTP_HOST") . "\\" . "uploads\\" . $info->getSaveName();
            $proData['img'] = $save;
        }
        $project->where('id','=',$data['id'])->update($proData);

        //数组转换操作处理
        $result = explode(',',$data['ProOpt']);
        $result[0] = substr($result[0],1);
        $result[count($result)-1] = substr($result[count($result)-1],0,-1);
        $data['ProOpt'] = $result;

        //选项表的数据
        $option->where('project_id','=',$data['id'])->delete();
        foreach ($data['ProOpt'] as $value){
            $option->where('project_id','=',$data['id'])->insert(['project_id' => $data['id'],'content' => $value]);
        }
        return $this->returnJson(self::OK,'修改投票项目成功');
    }

}